using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Storage;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
namespace MicroMsg.UI.Page
{
	public class SnsEditGroupsPage : BasePage
	{
		private const string TAG = "SnsEditGroupsPage";
		private const int cNPageArgCnt = 0;
		private EventWatcher mWatchSnsGroup;
		private object mObjectWatcher = new object();
		private List<SnsTagInfo> mCurGroups = new List<SnsTagInfo>();
		internal Grid LayoutRoot;
		internal StackPanel panelCustomGroups;
		internal ClickableLayout btnCreateNew;
		private bool _contentLoaded;
		public SnsEditGroupsPage()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.Page_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.Page_Unloaded));
		}
		private void Page_Loaded(object sender, RoutedEventArgs e)
		{
			Log.smethod_5("SnsEditGroupsPage", "Page_Loaded");
		}
		private void Page_Unloaded(object sender, RoutedEventArgs e)
		{
			Log.smethod_5("SnsEditGroupsPage", "Page_Unloaded");
		}
		protected override void OnPageIn(object[] object_0)
		{
			Log.smethod_5("SnsEditGroupsPage", "OnPageIn");
			this.registerEvent();
			this.initUI();
		}
		protected override void OnPageOut()
		{
			Log.smethod_5("SnsEditGroupsPage", "OnPageOut");
			this.unRegisterEvent();
		}
		protected override void OnPageBack()
		{
			Log.smethod_5("SnsEditGroupsPage", "OnPageBack");
		}
		private void registerEvent()
		{
			if (this.mWatchSnsGroup == null)
			{
				this.mWatchSnsGroup = new EventWatcher(this.mObjectWatcher, null, new EventHandlerDelegate(this.OnSnsGroup));
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_SNSTAG_ADD, this.mWatchSnsGroup);
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_SNSTAG_DEL, this.mWatchSnsGroup);
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_SNSTAG_MODIFY, this.mWatchSnsGroup);
			}
		}
		private void unRegisterEvent()
		{
			if (this.mWatchSnsGroup != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_SNSTAG_ADD, this.mWatchSnsGroup);
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_SNSTAG_DEL, this.mWatchSnsGroup);
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_SNSTAG_MODIFY, this.mWatchSnsGroup);
				this.mWatchSnsGroup = null;
			}
		}
		private void OnSnsGroup(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null || evtArgs.mObject == null)
			{
				return;
			}
			switch (evtArgs.mEventID)
			{
			case EventConst.ON_STORAGE_SNSTAG_ADD:
			{
				List<SnsTagInfo> listObject = evtArgs.getListObject<SnsTagInfo>();
				this.addGroupList(listObject);
				return;
			}
			case EventConst.ON_STORAGE_SNSTAG_DEL:
			{
				List<SnsTagInfo> listObject2 = evtArgs.getListObject<SnsTagInfo>();
				this.removeGroupList(listObject2);
				return;
			}
			case EventConst.ON_STORAGE_SNSTAG_MODIFY:
			{
				List<SnsTagInfo> listObject3 = evtArgs.getListObject<SnsTagInfo>();
				this.modifyGroupList(listObject3);
				return;
			}
			default:
				return;
			}
		}
		private void btnCreateNew_Click(object sender, RoutedEventArgs e)
		{
			AddContactsPage.GotoThisPage(new AddContactsPageArgs
			{
				mDlgGetContacts = new MMStandardUIEvent(this.addConPageDlg),
				mType = ContactsViewType.CONTACT_VIEW_CHECKTOADD,
				mNFilterKind = 6,
				mBCanImportFromGroup = true
			});
		}
		private void addConPageDlg(object obj1, object obj2)
		{
			if (obj2 == null || obj1 == null)
			{
				return;
			}
			List<Contact> list;
			if (obj2 is Contact)
			{
				Contact contact = obj2 as Contact;
				if (contact == null)
				{
					return;
				}
				list = ChatRoomMgr.getChatRoomMemberContactList(contact.strUsrName);
			}
			else
			{
				list = (obj2 as List<Contact>);
			}
			if (list == null || list.get_Count() == 0)
			{
				return;
			}
			AddContactsPageCBArg addContactsPageCBArg = obj1 as AddContactsPageCBArg;
			if (addContactsPageCBArg == null)
			{
				return;
			}
			string text = "";
			if (addContactsPageCBArg.mConIfItsGroup != null)
			{
				text = ContactHelper.getDisplayName(addContactsPageCBArg.mConIfItsGroup.strUsrName, addContactsPageCBArg.mConIfItsGroup);
			}
			addContactsPageCBArg.mStrPagePath = "/Source/Plugin/Sns/Common/UI/SnsGroup/SnsGroupDetailPage.xaml";
			addContactsPageCBArg.mPageArgs = new object[]
			{
				list,
				text
			};
		}
		private void initUI()
		{
			List<SnsTagInfo> list = StorageMgr.snsTag.getList();
			this.addGroupList(list);
		}
		private void addGroupList(List<SnsTagInfo> listGroup)
		{
			if (listGroup == null)
			{
				return;
			}
			using (List<SnsTagInfo>.Enumerator enumerator = listGroup.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					SnsTagInfo current = enumerator.get_Current();
					if (current != null && SnsGroupMgr.IsCustomGroup(current))
					{
						int nIndex = -1;
						if (!this.isInCurGroupList(current, ref nIndex))
						{
							this.insertGroupToCurList(nIndex, current);
						}
					}
				}
			}
		}
		private void insertGroupToCurList(int nIndex, SnsTagInfo snsTagInfo_0)
		{
			if (nIndex < 0)
			{
				return;
			}
			if (snsTagInfo_0 == null)
			{
				return;
			}
			this.mCurGroups.Insert(nIndex, snsTagInfo_0);
			SnsGroupAbstract snsGroupAbstract = new SnsGroupAbstract();
			snsGroupAbstract.mSnsGroup = snsTagInfo_0;
			this.panelCustomGroups.get_Children().Insert(nIndex, snsGroupAbstract);
		}
		private void modifyGroupList(List<SnsTagInfo> listGroup)
		{
			if (listGroup == null)
			{
				return;
			}
			int num = 0;
			using (List<SnsTagInfo>.Enumerator enumerator = listGroup.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					SnsTagInfo current = enumerator.get_Current();
					if (this.isInCurGroupList(current, ref num))
					{
						this.mCurGroups.set_Item(num, current);
						SnsGroupAbstract snsGroupAbstract = this.panelCustomGroups.get_Children().get_Item(num) as SnsGroupAbstract;
						if (snsGroupAbstract != null)
						{
							snsGroupAbstract.mSnsGroup = current;
						}
					}
				}
			}
		}
		private void removeGroupList(List<SnsTagInfo> listGroup)
		{
			if (listGroup == null)
			{
				return;
			}
			int num = 0;
			using (List<SnsTagInfo>.Enumerator enumerator = listGroup.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					SnsTagInfo current = enumerator.get_Current();
					if (this.isInCurGroupList(current, ref num))
					{
						this.mCurGroups.RemoveAt(num);
						this.panelCustomGroups.get_Children().RemoveAt(num);
					}
				}
			}
		}
		private bool isInCurGroupList(SnsTagInfo group, ref int nIndex)
		{
			nIndex = 0;
			using (List<SnsTagInfo>.Enumerator enumerator = this.mCurGroups.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					SnsTagInfo current = enumerator.get_Current();
					int num = this.compSnsTag(group, current);
					if (num == 0)
					{
						bool result = true;
						return result;
					}
					if (num < 0)
					{
						bool result = false;
						return result;
					}
					nIndex++;
				}
			}
			return false;
		}
		private int compSnsTag(SnsTagInfo snsTagInfo_0, SnsTagInfo snsTagInfo_1)
		{
			if (snsTagInfo_0 == null)
			{
				return -1;
			}
			if (snsTagInfo_1 == null)
			{
				return 1;
			}
			if (snsTagInfo_0.nTagID == snsTagInfo_1.nTagID)
			{
				return 0;
			}
			if (snsTagInfo_0.nTagID < snsTagInfo_1.nTagID)
			{
				return -1;
			}
			return 1;
		}
		protected override void OnBackKeyPress(CancelEventArgs cancelEventArgs_0)
		{
		}
		public static void GotoThisPage()
		{
			ToolFunction.GotoPageForUserControl("/Source/Plugin/Sns/Common/UI/SnsGroup/SnsEditGroupsPage.xaml", null, false);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/Sns/Common/UI/SnsGroup/SnsEditGroupsPage.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.panelCustomGroups = (StackPanel)base.FindName("panelCustomGroups");
			this.btnCreateNew = (ClickableLayout)base.FindName("btnCreateNew");
		}
	}
}
